from io import BytesIO
from unittest import main, TestCase

from jd4._compare import compare_stream

def compare(a, b):
    return compare_stream(BytesIO(a), BytesIO(b))

class CompareTest(TestCase):
    def test_small(self):
        self.assertTrue(compare(b'', b''))
        self.assertTrue(compare(b'a', b'a'))
        self.assertFalse(compare(b'a', b'b'))
        self.assertTrue(compare(b'bar', b'bar'))
        self.assertFalse(compare(b'bar', b'baz'))
        self.assertFalse(compare(b'bar', b'baz'))

    def test_a_plus_b(self):
        answer = b'1 2\r\n'
        self.assertTrue(compare(answer, b'1 2'))
        self.assertTrue(compare(answer, b'1 2\n'))
        self.assertTrue(compare(answer, b'1 2\r'))
        self.assertTrue(compare(answer, b'1 2\r\n'))
        self.assertTrue(compare(answer, b'1 2 '))
        self.assertTrue(compare(answer, b'1 2 \n'))
        self.assertTrue(compare(answer, b'1 2 \r'))
        self.assertTrue(compare(answer, b'1 2 \r\n'))
        self.assertTrue(compare(answer, b'1  2'))
        self.assertTrue(compare(answer, b'1  2\n'))
        self.assertTrue(compare(answer, b'1  2\r'))
        self.assertTrue(compare(answer, b'1  2\r\n'))
        self.assertTrue(compare(answer, b'1  2 '))
        self.assertTrue(compare(answer, b'1  2 \n'))
        self.assertTrue(compare(answer, b'1  2 \r'))
        self.assertTrue(compare(answer, b'1  2 \r\n'))
        self.assertTrue(compare(answer, b' 1 2'))
        self.assertTrue(compare(answer, b' 1 2\n'))
        self.assertTrue(compare(answer, b' 1 2\r'))
        self.assertTrue(compare(answer, b' 1 2\r\n'))
        self.assertTrue(compare(answer, b' 1 2 '))
        self.assertTrue(compare(answer, b' 1 2 \n'))
        self.assertTrue(compare(answer, b' 1 2 \r'))
        self.assertTrue(compare(answer, b' 1 2 \r\n'))
        self.assertFalse(compare(answer, b'1 1'))
        self.assertFalse(compare(answer, b'1 1\n'))
        self.assertFalse(compare(answer, b'1 1\r'))
        self.assertFalse(compare(answer, b'1 1\r\n'))
        self.assertFalse(compare(answer, b'1 1 '))
        self.assertFalse(compare(answer, b'1 1 \n'))
        self.assertFalse(compare(answer, b'1 1 \r'))
        self.assertFalse(compare(answer, b'1 1 \r\n'))
        self.assertFalse(compare(answer, b' 1 1'))
        self.assertFalse(compare(answer, b' 1 1\n'))
        self.assertFalse(compare(answer, b' 1 1\r'))
        self.assertFalse(compare(answer, b' 1 1\r\n'))
        self.assertFalse(compare(answer, b' 1 1 '))
        self.assertFalse(compare(answer, b' 1 1 \n'))
        self.assertFalse(compare(answer, b' 1 1 \r'))
        self.assertFalse(compare(answer, b' 1 1 \r\n'))
        self.assertFalse(compare(answer, b'2 2'))
        self.assertFalse(compare(answer, b'2 2\n'))
        self.assertFalse(compare(answer, b'2 2\r'))
        self.assertFalse(compare(answer, b'2 2\r\n'))
        self.assertFalse(compare(answer, b'2 2 '))
        self.assertFalse(compare(answer, b'2 2 \n'))
        self.assertFalse(compare(answer, b'2 2 \r'))
        self.assertFalse(compare(answer, b'2 2 \r\n'))
        self.assertFalse(compare(answer, b' 2 2'))
        self.assertFalse(compare(answer, b' 2 2\n'))
        self.assertFalse(compare(answer, b' 2 2\r'))
        self.assertFalse(compare(answer, b' 2 2\r\n'))
        self.assertFalse(compare(answer, b' 2 2 '))
        self.assertFalse(compare(answer, b' 2 2 \n'))
        self.assertFalse(compare(answer, b' 2 2 \r'))
        self.assertFalse(compare(answer, b' 2 2 \r\n'))
        self.assertFalse(compare(answer, b'2 1'))
        self.assertFalse(compare(answer, b'2 1\n'))
        self.assertFalse(compare(answer, b'2 1\r'))
        self.assertFalse(compare(answer, b'2 1\r\n'))
        self.assertFalse(compare(answer, b'2 1 '))
        self.assertFalse(compare(answer, b'2 1 \n'))
        self.assertFalse(compare(answer, b'2 1 \r'))
        self.assertFalse(compare(answer, b'2 1 \r\n'))
        self.assertFalse(compare(answer, b' 2 1'))
        self.assertFalse(compare(answer, b' 2 1\n'))
        self.assertFalse(compare(answer, b' 2 1\r'))
        self.assertFalse(compare(answer, b' 2 1\r\n'))
        self.assertFalse(compare(answer, b' 2 1 '))
        self.assertFalse(compare(answer, b' 2 1 \n'))
        self.assertFalse(compare(answer, b' 2 1 \r'))
        self.assertFalse(compare(answer, b' 2 1 \r\n'))
        self.assertFalse(compare(answer, b'12'))

    def test_large(self):
        self.assertTrue(compare(b'a' * 1048576, b'a' * 1048576))
        self.assertFalse(compare(b'a' * 1048576, b'a' * 1048575 + b'b'))
        self.assertTrue(compare(b'a' * 1048576 + b' ' + b'b' * 1048576 + b'\r\n',
                                b'a' * 1048576 + b' ' * 1048576 + b'b' * 1048576))

if __name__ == '__main__':
    main()
